from django.urls import path
from api.Login.views import login, logins
from api.Auth.views import Auths
from api.Materials.views import MaterialsHistorys,Materialss

from api.User.views import UserList
from api.Position.views import Positions
from api.Project.views import Projects
from api.Template.views import Template_port
from api.place.views import PlaceView
from api.modules.views import Modules_port
from api.recoder.views import RecoderView
from api.Remarks.views import RemarksView
from api.count.views import Count
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
from constr_budgets.models import Day, Project, Templates, Record,Remarks


def tick():
    print("验证ing。。。")

    # if "00:01">datetime.now().strftime("%H:%M")>="00:00":
    print(datetime.now().strftime("%Y-%m-%d"))
    # print(datetime.now())
    if not len(Day.objects.filter(time__gte=datetime.now().strftime("%Y-%m-%d"))):
        print("新的一天了")

        aimday = Day.objects.create(time=datetime.now().strftime("%Y-%m-%d"))
        aimProject = Project.objects.all().values()
        for pro in aimProject:
            template_id = Project.objects.get(id=pro['id']).templates_id
            aimTemplate = Templates.objects.get(id=template_id)
            placelist = list(aimTemplate.place_set.all().values())
            moduleslist = list(aimTemplate.modules_set.all().values())
            for i in moduleslist:
                Remarks.objects.create(remark="",day_id=aimday.id,modules_id=i['id'],project_id=pro['id'])
                for j in placelist:
                    newRecoder = Record.objects.create(project_id=pro['id'], day_id=aimday.id)
                    newRecoder.modules.add(i['id'])
                    newRecoder.place.add(j['id'])


scheduler = BackgroundScheduler()
scheduler.add_job(tick, 'interval', minutes=1,timezone='Asia/Shanghai')
scheduler.start()

urlpatterns = [
    path("login", login),
    path("logins", logins),
    path("auth", Auths.as_view()),
    path("user", UserList.as_view()),
    path("position", Positions.as_view()),
    path('project', Projects.as_view()),
    path('template', Template_port.as_view()),
    path('place', PlaceView.as_view()),
    path('models', Modules_port.as_view()),
    # path("recoder", RecoderView.as_view()),
    path("recoder/", RecoderView.as_view()),
    path("remarks/", RemarksView.as_view()),
    path("materials",Materialss.as_view()),
    path("materialsHistory",MaterialsHistorys.as_view()),
    path("count",Count.as_view())
]
